package com.zzq.my.jetpacks.jectpackapp.net.submitfeedback

import com.zzq.my.jetpacks.jectpackapp.net.ApiService
import com.zzq.my.jetpacks.jectpackapp.net.ApiUrl
import com.zzq.my.jetpacks.net.Util.ImageFileter
import com.zzq.my.jetpacks.net.Util.RxPartMapUtils
import com.zzq.my.jetpacks.net.base.BaseRepository
import com.zzq.my.jetpacks.net.base.RetrofitClient
import com.zzq.my.jetpacks.net.entity.ApiResponse
import okhttp3.MultipartBody
import rxhttp.RxHttp
import rxhttp.awaitResult
import rxhttp.toResponse
import java.io.File

/**
 * 描述：class describe
 * 作者：zhuangzeqin
 * 时间: 2022/1/20-13:47
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class SubmitFeedbackRepo : BaseRepository() {
    private val mApiService by lazy { RetrofitClient.getInstance().initConfig("http://cs-ys-agentapi2.51ydmw.com/").create(ApiService::class.java) }
    //存放图片的集合
    private var parts = mutableListOf<MultipartBody.Part>()
    private var fileMap = mutableMapOf<String, File>()

    suspend fun submitFeedback(
        problemType: String,
        complainterType: String,
        content: String,
        mobileNo: String,
        filePaths: List<String>
    ): ApiResponse<String> {
//        val requestMap = mutableMapOf<String, String>()//可变的map
        val requestMap = getPubParams()//可变的map
        requestMap["problemType"] = problemType
        requestMap["complainterType"] = complainterType
        requestMap["content"] = content
        requestMap["mobileNo"] = mobileNo

        parts.clear()
        //图片遍历
        filePaths.forEach{
            val file = File(it)
            if (file.exists()) {
                val fileName = file.name
                val isImage: Boolean = ImageFileter.checkImage(fileName) //附近传过来只能是图片png,jpg,gif
                if (isImage) {//是图片
                    val toRequestBodyOfPart = RxPartMapUtils.toRequestBodyOfPart(file)
                    parts.add(toRequestBodyOfPart)//添加进来
                }
            }
        }
        //执行请求
        return executeHttp {
            mApiService.submitFeedback(requestMap,parts)
        }
    }
    suspend fun submitFeedbackRxHttp(problemType: String,
                                 complainterType: String,
                                 content: String,
                                 mobileNo: String,
                                 filePaths: List<String>):Result<String>
    {
        val requestMap = getPubParams()//可变的map
        requestMap["problemType"] = problemType
        requestMap["complainterType"] = complainterType
        requestMap["content"] = content
        requestMap["mobileNo"] = mobileNo
        //图片遍历
        filePaths.forEach{
            val file = File(it)
            if (file.exists()) {
                val fileName = file.name
                val isImage: Boolean = ImageFileter.checkImage(fileName) //附近传过来只能是图片png,jpg,gif
                if (isImage) {//是图片
                    fileMap[fileName]=file
                }
            }
        }
        /**
         * 参数+文件提交是表单提交方式，设置头部isSkip=10086
         */
        return RxHttp.postForm(ApiUrl.SUBMITFEEDBACK).addHeader("isSkip","10086")
            .addAll(requestMap)
            .addFiles(fileMap)
            .toResponse<String>()
            .awaitResult()
    }

}